﻿using System.Collections.Generic;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;

namespace Common
{
   public class JWTHelp
    {
       public static string secret = ConfigHelper.GetValue("Secret");
       public static string SetJwtEncode(Dictionary<string,object> payload) {
            IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
            IJsonSerializer serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
            var token = encoder.Encode(payload,secret);
            return token;
       }
        public static Dictionary<string,object> SetJwtDecode(string token) {
            IJsonSerializer serializer = new JsonNetSerializer();
            IDateTimeProvider provider = new UtcDateTimeProvider();
            IJwtValidator validator = new JwtValidator(serializer, provider);
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);
            JsonNetSerializer jns = new JsonNetSerializer();
            var json = decoder.Decode(token, secret, verify: true);//token为之前生成的字符串
            return CommonUtils.DeserializeStringToDictionary<string,object>(json);
        }

    
    }
   
}
